package rpg.visual.menu.skill;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import launch.Launch;
import pgui.PVisual;
import pgui.visual.PComponent;
import pgui.visual.PImage;
import rpg.action.Skill;
import rpg.player.Player;
import rpg.visual.VisualImage;
import rpg.visual.menu.VisualMenuSkills;

public class VisualMenuSkill extends PComponent {
	private Skill skill;
	private PImage image = null;
	protected boolean hover = false, clicked = false;

	// Creation
	public VisualMenuSkill(PVisual visual) {
		super(visual);
		this.setWidth(36);
		this.setHeight(36);
	}

	// Player
	private Player player() {
		return Launch.getLauncher().getPlayer();
	}
	
	// Skill
	public void castSkill() {
		if (getSkill() == null)
			return;
		
		player().cast(getSkill());
	}

	// Mouse Events
	@Override
	public void onMouseMoved(MouseEvent event, MouseEvent lastEvent) {
		super.onMouseMoved(event, lastEvent);
		hover = true;
		setCursor(Cursor.HAND_CURSOR);
	}

	@Override
	public void onMouseDragged(MouseEvent event, MouseEvent lastEvent) {
		super.onMouseDragged(event, lastEvent);
		hover = true;
		clicked = true;
		setCursor(Cursor.HAND_CURSOR);
	}

	@Override
	public void onMousePressed(MouseEvent event) {
		super.onMousePressed(event);
		clicked = true;
	}

	@Override
	public void onMouseReleased(MouseEvent event) {
		super.onMouseReleased(event);
		clicked = false;

		castSkill();
	}

	@Override
	public void onMouseExited(MouseEvent event, MouseEvent lastEvent) {
		super.onMouseExited(event, lastEvent);
		hover = false;
		clicked = false;
		setCursor(Cursor.DEFAULT_CURSOR);
	}

	// Paint Events
	@Override
	public void onPaint(Graphics2D g) {
		super.onPaint(g);

		// Background
		g.setColor(Color.white);
		if (clicked) {
			VisualImage.opacity(g, 0.55f);
		} else if (hover)
			VisualImage.opacity(g, 0.25f);
		else
			VisualImage.opacity(g, 0.1f);

		// Background
		g.fill3DRect(0, 0, this.getLastWidth(), this.getLastHeight(), true);

		// Border
		g.setColor(Color.darkGray);
		VisualImage.opacity(g, 0.25f);
		g.draw3DRect(0, 0, this.getLastWidth(), this.getLastHeight(), true);

		VisualImage.opacity(g, 1);
		
		if (image != null) {
			g.translate(2, 2);
			image.draw(g);
			g.translate(-2, -2);
			
			if (getSkill().getLastUse() != 0L) {
				int percent = (int) ((System.currentTimeMillis() - getSkill()
						.getLastUse()) * 100 / getSkill().getData().getDelay());
				if (percent < 100) {
					VisualImage.opacity(g, 0.5f);
					int relativeHeight = this.getLastHeight() * percent / 100;
					g.fillRect(0, relativeHeight, this.getLastWidth(),
							this.getLastHeight() - relativeHeight);
				}
			}
		}

		VisualImage.opacity(g, 0.25f);
		g.setFont(g.getFont().deriveFont(15f));
		
		g.setColor(Color.darkGray);
		g.drawString("F"
				+ (((VisualMenuSkills) this.getParent()).index(this) + 1), 11,
				26);
		
		g.setColor(Color.white);
		g.drawString("F"
				+ (((VisualMenuSkills) this.getParent()).index(this) + 1), 9,
				24);
	}

	// Get & Set
	public Skill getSkill() {
		return skill;
	}

	public void setSkill(Skill skill) {
		this.skill = skill;

		this.setImage((PImage) skill.getData().getThumb().getFrame(0));
		this.setImage(this.getImage().instance());
		this.getImage().resize(33, 33);
	}

	public PImage getImage() {
		return image;
	}

	public void setImage(PImage image) {
		this.image = image;
	}
}
